/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics.support;

import forhelp.PrintArray;

/**
 *
 * @author asus
 */
public class WorkingAreaConverter {

    public static int[][] convertToindexes(double[] wavelength, double[] timedelay, double[][] workingArea, String typeYaxis) {
        int[][] var = new int[2][2];

        var[0][0] = closestValue(wavelength, workingArea[0][0]);
        var[0][1] = closestValue(wavelength, workingArea[0][1]);

        if ("numbers".equals(typeYaxis)) {
            var[1][0] = (int) Math.round(workingArea[1][0]);
            var[1][1] = (int) Math.round(workingArea[1][1]);
        }
        if ("real".equals(typeYaxis)) {
            var[1][0] = closestValue(timedelay, workingArea[1][0]);
            var[1][1] = closestValue(timedelay, workingArea[1][1]);
        }
        return var;
    }

    private static int closestValue(double[] array, double desiredNumber) {
        int nearest = -1;
        double bestDistanceFoundYet = 1000;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == desiredNumber) {
                return i;
            } else {
                double d = Math.abs(desiredNumber - array[i]);
                if (d < bestDistanceFoundYet) {
                    nearest = i;
                    bestDistanceFoundYet = d;
                }
            }
        }
        return nearest;
    }
}
